package cn.iocoder.yudao.module.acm.dal.mysql.stock;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.acm.controller.admin.stock.vo.out.AcmStockOutPageReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.stock.AcmStockOutDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.stock.AcmStockOutItemDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInItemDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * ERP 其它出库单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface AcmStockOutMapper extends BaseMapperX<AcmStockOutDO> {

    default PageResult<AcmStockOutDO> selectPage(AcmStockOutPageReqVO reqVO) {
        MPJLambdaWrapperX<AcmStockOutDO> query = new MPJLambdaWrapperX<AcmStockOutDO>()
                .likeIfPresent(AcmStockOutDO::getNo, reqVO.getNo())
                .eqIfPresent(AcmStockOutDO::getCustomerId, reqVO.getCustomerId())
                .betweenIfPresent(AcmStockOutDO::getOutTime, reqVO.getOutTime())
                .eqIfPresent(AcmStockOutDO::getStatus, reqVO.getStatus())
                .likeIfPresent(AcmStockOutDO::getRemark, reqVO.getRemark())
                .eqIfPresent(AcmStockOutDO::getCreator, reqVO.getCreator())
                .orderByDesc(AcmStockOutDO::getId);
        if (reqVO.getWarehouseId() != null || reqVO.getProductId() != null) {
            query.leftJoin(AcmStockOutItemDO.class, AcmStockOutItemDO::getOutId, AcmStockOutDO::getId)
                    .eq(reqVO.getWarehouseId() != null, AcmStockOutItemDO::getWarehouseId, reqVO.getWarehouseId())
                    .eq(reqVO.getProductId() != null, AcmStockOutItemDO::getProductId, reqVO.getProductId())
                    .groupBy(AcmStockOutDO::getId); // 避免 1 对多查询，产生相同的 1
        }
        return selectJoinPage(reqVO, AcmStockOutDO.class, query);
    }


//    default PageResult<AcmStockOutDO> selectAppPage(AppAcmStockOutPageReqVO reqVO) {
//        MPJLambdaWrapperX<AcmStockOutDO> query = new MPJLambdaWrapperX<AcmStockOutDO>()
//                .likeIfPresent(AcmStockOutDO::getNo, reqVO.getNo())
//                .eqIfPresent(AcmStockOutDO::getCustomerId, reqVO.getCustomerId())
//                .betweenIfPresent(AcmStockOutDO::getOutTime, reqVO.getOutTime())
//                .eqIfPresent(AcmStockOutDO::getStatus, reqVO.getStatus())
//                .likeIfPresent(AcmStockOutDO::getRemark, reqVO.getRemark())
//                .eqIfPresent(AcmStockOutDO::getCreator, reqVO.getCreator());
//        if (reqVO.getWarehouseId() != null || reqVO.getProductId() != null || reqVO.getProductName() != null) {
//            query.leftJoin(AcmStockOutItemDO.class, AcmStockOutItemDO::getOutId, AcmStockOutDO::getId)
//                    .eq(reqVO.getWarehouseId() != null, AcmStockOutItemDO::getWarehouseId, reqVO.getWarehouseId())
//                    .eq(reqVO.getProductId() != null, AcmStockOutItemDO::getProductId, reqVO.getProductId());
//
//            if (reqVO.getProductName() != null) {
//                query.leftJoin(ErpProductDO.class, ErpProductDO::getId, ErpPurchaseInItemDO::getProductId)
//                        .likeIfPresent(ErpProductDO::getName, reqVO.getProductName());
//            }
//
//            query.groupBy(ErpPurchaseInDO::getId); // 避免 1 对多查询，产生相同的 1
//        }
//        if(reqVO.getOrderByType() != null){
//            if(reqVO.getOrderByType() == 1){
//                query.orderByDesc(AcmStockOutDO::getOutTime);
//            }else if(reqVO.getOrderByType() == 2){
//                query.orderByAsc(AcmStockOutDO::getOutTime);
//            }
//        }else {
//            query.orderByDesc(AcmStockOutDO::getCreateTime);
//        }
//
//        return selectJoinPage(reqVO, AcmStockOutDO.class, query);
//    }

    default int updateByIdAndStatus(Long id, Integer status, AcmStockOutDO updateObj) {
        return update(updateObj, new LambdaUpdateWrapper<AcmStockOutDO>()
                .eq(AcmStockOutDO::getId, id).eq(AcmStockOutDO::getStatus, status));
    }

    default AcmStockOutDO selectByNo(String no) {
        return selectOne(AcmStockOutDO::getNo, no);
    }

}
